import { bind_platform, bind_platform_select } from '@/services/instiution';
import { ActionType, ProForm, ProFormSelect, ProFormTextArea } from '@ant-design/pro-components';
import { Divider, message, Modal } from 'antd';

interface Iprops {
  isShowAddModal: (show: boolean) => void;
  isAddModalOpen: boolean;
  actionRef: React.MutableRefObject<ActionType | undefined>;
}
const AddInstitution = (props: Iprops) => {
  const { isAddModalOpen, isShowAddModal, actionRef } = props;

  // 获取可绑定机构列表下拉框
  const get_bind_platform_select = async () => {
    let params = await bind_platform_select();
    // console.log('可绑定机构', params);
    let res: any = [];
    params.data.map((item: { name: string; id: string }) => {
      let temp: any = {};
      temp['label'] = item.name;
      temp['value'] = parseInt(item.id);
      res.push(temp);
    });
    return res;
  };
  // 新增绑定机构
  const add_bind_platform = async (values: any) => {
    console.log(values);
    let params: any = await bind_platform(values);
    // console.log('新增', params);
    if (params.msg == 'ok') {
      //关闭弹窗
      actionRef.current?.reload();
      message.success('添加成功');
      isShowAddModal(false);
      //刷新列表
    } else {
      message.error(params.msg);
    }
  };
  return (
    <Modal
      title="添加关联机构"
      centered
      open={isAddModalOpen}
      onCancel={() => isShowAddModal(false)}
      width={500}
      footer={null}
    >
      <Divider />
      <ProForm<{
        jigou: string;
        reason: string;
      }>
        onReset={() => isShowAddModal(false)}
        onFinish={(value) => add_bind_platform(value)}
        submitter={{
          // 配置按钮文本
          searchConfig: {
            resetText: '取消',
            submitText: '确定',
          },
          resetButtonProps: {
            style: {
              display: 'block',
              position: 'absolute',
              left: 'calc(50% - 107px)',
            },
          },
          submitButtonProps: {
            style: {
              display: 'block',
              position: 'absolute',
              right: 'calc(50% - 107px)',
            },
          },
        }}
      >
        <ProFormSelect
          name="platform_id"
          label="机构"
          request={async () => get_bind_platform_select()}
          placeholder="请选择关联机构"
          rules={[{ required: true, message: '请选择关联机构' }]}
          fieldProps={{
            showSearch: true,
            filterOption: (input: any, option: any) =>
              (option?.label ?? '').toLowerCase().includes(input.toLowerCase())
          }}

        />
        <ProFormTextArea
          name="reason"
          label="申请绑定原因"
          placeholder="请填写申请绑定原因(必填)"
          rules={[
            { required: true, message: '请填写申请绑定原因' },
            {
              max: 100,
              message: '字数最多100个字符',
            },
            { pattern: /(^\S)((.)*\S)?(\S*$)/, message: '不能输入空格' },
          ]}
        />
        <br />
      </ProForm>
      <br />
    </Modal>
  );
};
export default AddInstitution;
